package com.djinfo.work.util;


import java.io.UnsupportedEncodingException;
import java.util.*;

public class EqualsUtils {

	public static boolean equals(Integer i1, Integer i2) {
		if (i1 == i2) {
			return true;
		} else {
			if (i1 != null) {
				return i1.equals(i2);
			} else { // i1 == null
				if (i2 != null) {
					return i2.equals(i1);
				} else { // i2 == null
					return true;
				}
			}
		}
	}

	public static Object[] listandObject(Object... objects) {
		List<Object> list = new ArrayList<Object>();
		for (int i = 0; i < objects.length; i++) {
			if (!objects[i].equals("")) {
				list.add(objects[i]);
			}
		}
		Object[] object = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			object[i] = "%" + list.get(i) + "%";
		}
		return object;
	}

	public static String replaceLike(String name) throws UnsupportedEncodingException {
		if (name == null) {
			return name;
		}
		StringBuffer outStrBuf = new StringBuffer("");
		String Tstr = "";
		byte[] b = null;
		for (int i = 0; i < name.length(); i++) {
			Tstr = name.substring(i, i + 1);
			// 全角空格转换成半角空格
			if (Tstr.equals("　")) {
				outStrBuf.append(" ");
				continue;
			}
			if(Tstr.equals(" ")){
				outStrBuf.append(" ");
				continue;
			}
			if(Tstr.equals("￥")){
				outStrBuf.append("￥");
				continue;
			}
			if(Tstr.equals("\\")){
				outStrBuf.append("\\\\\\\\");
				continue;
			}
			b = Tstr.getBytes("unicode");
			// 得到 unicode 字节数据
			if (b[2] == -1) {
				// 表示全角？
				b[3] = (byte) (b[3] + 32);
				b[2] = 0;
				outStrBuf.append(new String(b, "unicode"));
			} else {
				outStrBuf.append(Tstr);
			}
		} // end for.
		String replaceLike = outStrBuf.toString().replace("%", "\\%").replace("_", "\\_").replace("$", "\\$");
		return replaceLike.trim();
	}

	public static String replaceLike1(String name) throws UnsupportedEncodingException {
		if (name == null) {
			return name;
		}
		StringBuffer outStrBuf = new StringBuffer("");
		String Tstr = "";
		byte[] b = null;
		for (int i = 0; i < name.length(); i++) {
			Tstr = name.substring(i, i + 1);
			// 全角空格转换成半角空格
			if (Tstr.equals("　")) {
				outStrBuf.append(" ");
				continue;
			}
			if(Tstr.equals(" ")){
				outStrBuf.append(" ");
				continue;
			}
			if(Tstr.equals("￥")){
				outStrBuf.append("￥");
				continue;
			}
			b = Tstr.getBytes("unicode");
			// 得到 unicode 字节数据
			if (b[2] == -1) {
				// 表示全角？
				b[3] = (byte) (b[3] + 32);
				b[2] = 0;
				outStrBuf.append(new String(b, "unicode"));
			} else {
				outStrBuf.append(Tstr);
			}
		} // end for.
		return outStrBuf.toString();
	}

	public static String trim(String s) {
		String result = "";
		if (null != s && !"".equals(s)) {
			result = s.replaceAll("^[　*| *| *|//s*]*", "").replaceAll("[　*| *| *|//s*]*$", "");
		}
		return result;
	}


	public static void main(String[] args) throws UnsupportedEncodingException {
	System.out.print(replaceLike("#￥%……&*（"))	;
	}

	/**
	 * 获取后缀名
	 *
	 * @param fileName
	 * @return
	 */
	public static String extNames(String fileName) {
		String extName = "";
		if ((fileName != null) && (fileName.length() > 0)) {
			int dot = fileName.lastIndexOf('.');
			if ((dot > -1) && (dot < (fileName.length()))) {
				extName = fileName.substring(fileName.lastIndexOf(".") + 1);
			}
		}
		return extName;
	}

	public static String Newpath(String filelook, String fileName,String uploadFileUrlPrefix,String filepath,String filepathHtml) {
		String extName = extNames(fileName);
		String[] filelooks = filelook.split(",");
		String  path="";
		for (String fileslook : filelooks) {
			if (fileslook.toLowerCase().equals(extName)) {
				if (extName.equals("xls") || extName.equals("xlsx")||extName.equals("docx") ||extName.equals("doc")) {
					String newName=newFile(fileName);
					return uploadFileUrlPrefix+"/"+filepathHtml+"/"+newName +".html";
				}else{
					String newName=newFile(fileName);
					return path=uploadFileUrlPrefix+"/"+fileName;
				}
			}
		}
		return path;
	}


	/**
	 * 获取最新的文件名newFile
	 * @param fileName
	 * @return
	 */
	public static String newFile(String fileName){
		String newfileName="";
		if ((fileName != null) && (fileName.length() > 0)) {
			int dot = fileName.indexOf('.');
			if ((dot > -1) && (dot < (fileName.length()))) {
				newfileName = fileName.substring(0, fileName.indexOf("."));
			}
		}
		return newfileName;
	}

	public  static String newNodeId(String nodeId){
		String node="";
		if(!nodeId.isEmpty()){
			String []nodes=nodeId.split(",");
			StringBuffer sb=new StringBuffer("");
			for(String nd:nodes){
				if(nd.indexOf("10000")==-1){
					if(nd.indexOf("system")>-1) {
						Integer index = nd.indexOf("-");
						sb.append(nd.substring(0, index) + ",");
					}
				}
			}
			node=sb.toString();
			if(node.endsWith(",")){
				node=node.substring(0,node.length()-1);
			}
		}
		return node;
	}
	public  static List<Integer> newRuleId(String nodeId){
		List<Integer> ruleList=new ArrayList<Integer>();
		if(!nodeId.isEmpty()){
			String []nodes=nodeId.split(",");
			StringBuffer sb=new StringBuffer("");
			for(String nd:nodes){
				if(nd.indexOf("rule") > -1 ) {
					Integer index = nd.indexOf("-");
					ruleList.add(Integer.parseInt(nd.substring(0, index)));
				}
			}
		}
		return ruleList;
	}
}
